#include <sys/syscall.h>
#include <mips/regdef.h>

	.text
	.align	2
	.globl 	mygetchar
	.ent	mygetchar

mygetchar:
	.frame $fp, 48, ra
	.set	noreorder
	.cpload	t9
	.set	reorder
	subu	sp, sp, 48

	.cprestore 32
	  #subu  sp,sp,24
	  sw	ra,40(sp)
	  sw	$fp,36(sp)
	  sw	gp,32(sp)
	  move	$fp,sp

	  li	v0,SYS_read
	  li	a0,0
	  la	a1,8($fp)
	  li	a2,1
	  syscall

	  bne	a3,zero,read_error
	  beq	v0, zero, eof
	  li	t0,1
	  bne	v0,t0, read_error
	  lb	v0,8($fp)
fing:	
	  move	sp,$fp
	  lw	ra,40(sp)
	  lw	$fp,36(sp)
	  lw	gp,32(sp)
	  addu	sp,sp,48
	  j	ra

eof:	
	  li	v0,-1
	  b	fing
	#.end	mygetchar


read_error:
	li	v0, SYS_exit
	li	a0, 1
	syscall
	
	.end	mygetchar
